package com.huonan.jb.tools.esuse.export.filter;

import cn.hutool.core.map.MapUtil;
import cn.hutool.json.JSONObject;

import java.util.*;

public class FieldsFilter {

    /**
     * @param source        es  导出数据
     * @param fieldMap      返回字段
     * @param isCustomField 是否自定义指定导出字段
     * @return Map
     */
    public static Map<String, Object> filter(JSONObject source,
                                             Map<String, Object> fieldMap, boolean isCustomField) {
        if (isCustomField) {
            String kwUrl = source.get("kw_url").toString();
            String urlTypes = source.get("kw_urlTypes", String[].class)[0];
            String sub1 = kwUrl.substring(0, kwUrl.lastIndexOf("/"));
            String subUrl = sub1.substring(sub1.lastIndexOf("/") + 1);
            String[] docDype = new String[1];
            if (subUrl.equals("video") && urlTypes.equals("photo")) {
                docDype[0] = "video";
            } else if (subUrl.equals("note") && urlTypes.equals("video")) {
                docDype[0] = "photo";
            } else {
                return MapUtil.empty();
            }
            String keyMap = "kw_urlTypes";
            fieldMap.put(keyMap, docDype);
        } else {
            for (String key : source.keySet()) {
                String value = source.getStr(key);
                fieldMap.put(key, value);
//                List<String> video = Collections.singletonList("photo");
//                fieldMap.put("kw_urlTypes", video);
//                fieldMap.put("text_storeName","EltaMD海外旗舰店");
            }
        }
        return fieldMap;
    }
}
